package com.luych.toolbox.data.service.service;

import com.luych.toolbox.common.enums.DataSourceStatus;
import com.luych.toolbox.data.service.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Service
public class DataStatsService {

    private final DataSourceDao dataSourceDao;
    private final DataProjectDao dataProjectDao;
    private final DataAgentDao dataAgentDao;
    private final DataBackupPlanDao dataBackupPlanDao;
    private final DataBackupHistoryDao dataBackupHistoryDao;
    private final DataRecoveryHistoryDao dataRecoveryHistoryDao;
    private final DataComparePlanDao dataComparePlanDao;
    private final DataCompareHistoryDao dataCompareHistoryDao;

    @Autowired
    public DataStatsService(DataSourceDao dataSourceDao,
                            DataProjectDao dataProjectDao,
                            DataAgentDao dataAgentDao,
                            DataBackupPlanDao dataBackupPlanDao,
                            DataBackupHistoryDao dataBackupHistoryDao,
                            DataRecoveryHistoryDao dataRecoveryHistoryDao,
                            DataComparePlanDao dataComparePlanDao,
                            DataCompareHistoryDao dataCompareHistoryDao) {
        this.dataSourceDao = dataSourceDao;
        this.dataProjectDao = dataProjectDao;
        this.dataAgentDao = dataAgentDao;
        this.dataBackupPlanDao = dataBackupPlanDao;
        this.dataBackupHistoryDao = dataBackupHistoryDao;
        this.dataRecoveryHistoryDao = dataRecoveryHistoryDao;
        this.dataComparePlanDao = dataComparePlanDao;
        this.dataCompareHistoryDao = dataCompareHistoryDao;
    }

    public Map<DataSourceStatus, Long> countDataSourceByStatus() {
        Map<DataSourceStatus, Long> result = new HashMap<>();
        for (DataSourceStatus dataSourceStatus : DataSourceStatus.values()) {
            Long count = dataSourceDao.countByStatus(dataSourceStatus);
            result.put(dataSourceStatus, count == null ? 0L : count);
        }
        return result;
    }

    public Long countDataSource() {
        return dataSourceDao.count();
    }

    public Long countDataProject() {
        return dataProjectDao.count();
    }

    public Long countDataAgent() {
        return dataAgentDao.count();
    }

    public Long countDataBackupPlan() {
        return dataBackupPlanDao.count();
    }

    public Long countDataBackupHistory() {
        return dataBackupHistoryDao.count();
    }

    public Long countDataRecoveryHistory() {
        return dataRecoveryHistoryDao.count();
    }

    public Long countComparePlan() {
        return dataComparePlanDao.count();
    }

    public Long countCompareHistory() {
        return dataCompareHistoryDao.count();
    }

    public Map<String, Long[]> sumDataTaskByDay() {
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.add(Calendar.DAY_OF_YEAR, -29);
        calendarStart.set(Calendar.HOUR, 0);
        calendarStart.set(Calendar.MINUTE, 0);
        calendarStart.set(Calendar.SECOND, 0);
        calendarStart.set(Calendar.MILLISECOND, 0);

        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.add(Calendar.DAY_OF_YEAR, -28);
        calendarStart.set(Calendar.HOUR, 0);
        calendarStart.set(Calendar.MINUTE, 0);
        calendarStart.set(Calendar.SECOND, 0);
        calendarStart.set(Calendar.MILLISECOND, 0);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");

        Map<String, Long[]> map = new LinkedHashMap<>();

        for (int i = 0; i < 30; i++) {
            long backupCount = dataBackupHistoryDao.countByStartDateBetween(calendarStart.getTime(), calendarEnd.getTime());
            long recoveryCount = dataRecoveryHistoryDao.countByStartDateBetween(calendarStart.getTime(), calendarEnd.getTime());
            long compareCount = dataCompareHistoryDao.countByStartDateBetween(calendarStart.getTime(), calendarEnd.getTime());

            String key = simpleDateFormat.format(calendarStart.getTime());

            map.put(key, new Long[]{backupCount, recoveryCount, compareCount});

            calendarStart.add(Calendar.DAY_OF_YEAR, 1);
            calendarEnd.add(Calendar.DAY_OF_YEAR, 1);
        }

        return map;
    }
}
